package com.codrim.cs.data.server.dao.mapper;

import com.codrim.cs.data.server.dao.entity.ReportRecordDO;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 *
 * @author Liang.Zhuge
 * @date 10/07/2017
 */
@Mapper
public interface ReportRecordMapper {

    @Select("select id, begin_time beginTime, end_time endTime, " +
            "create_time createTime, cost_in_ms costInMs, " +
            "zone " +
            "from report_record order by end_time desc, id desc limit 0, 1")
    ReportRecordDO findLastOne();

    @Insert("insert into report_record (" +
            "begin_time, end_time, create_time, cost_in_ms, zone" +
            ") " +
            "values (" +
            "#{param.beginTime}, #{param.endTime}, now(), #{param.costInMs}," +
            "#{param.zone}" +
            ")")
    void insert(@Param("param") ReportRecordDO reportRecordDO);

    @Delete("delete from report_record where begin_time >= ${beginTime} and end_time <= ${endTime} and zone = '${sZone}'")
    void deleteByBeginTimeEndTimeAndZone(@Param("beginTime") long startTime, @Param("endTime") long endTime,
            @Param("sZone") String zone);

    /**
     * 查询所有符合条件的报表记录，条件：begin_time = #{beginTime} and end_time = #{endTime} and zone = #{zone}
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param zone      时区
     * @return 匹配到的记录
     */
    @Select("SELECT "
            + "id, begin_time beginTime, end_time endTime, create_time createTime, cost_in_ms costInMs, zone "
            + "FROM report_record "
            + "WHERE begin_time = #{beginTime} AND end_time = #{endTime} AND zone = #{zone}")
    List<ReportRecordDO> findAllByBeginTimeAndEndTimeAndZone(@Param("beginTime") long beginTime,
            @Param("endTime") long endTime, @Param("zone") String zone);
}
